import cv2
import numpy as np


def callback():
    pass


# 创建窗口
cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL)

# 创建trackbar
cv2.createTrackbar('R', 'trackbar', 0, 255, callback)
cv2.createTrackbar('G', 'trackbar', 0, 255, callback)
cv2.createTrackbar('B', 'trackbar', 0, 255, callback)

img = np.zeros((480, 460, 3), np.uint8)
while True:
    cv2.imshow('trackbar', img)
    # 获取颜色
    r = cv2.getTrackbarPos('R', 'trackbar')
    g = cv2.getTrackbarPos('G', 'trackbar')
    b = cv2.getTrackbarPos('B', 'trackbar')
    # 图片更改颜色
    img[:] = [b, g, r]
    # 点击键盘Q退回
    key = cv2.waitKey(1)
    if (key & 0xFF == ord('q')):
        break

# 释放窗口资源
cv2.destroyAllWindows()
